from django.contrib.auth.models import User
from django.db import models
from django.template.loader import render_to_string


class Link(models.Model):
    STATUS_NORMAL = 1
    STATUS_DELETE = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, 'Normal'),
        (STATUS_DELETE, 'Delete')
    )
    title = models.CharField(max_length=50, verbose_name='Title')
    href = models.URLField(verbose_name='link')
    status = models.PositiveIntegerField(default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name='Status')
    weight = models.PositiveIntegerField(default=1, choices=zip(range(1, 6), range(1, 6)), verbose_name='weight')
    owner = models.ForeignKey(User, verbose_name='Author', on_delete=models.DO_NOTHING)
    created_time = models.DateTimeField(auto_now_add=True, editable=False, verbose_name='Create Time')

    class Meta:
        verbose_name = verbose_name_plural = 'link'


class SideBar(models.Model):
    DISPLAY_HTML = 1
    DISPLAY_LATEST = 2
    DISPLAY_HOT = 3
    DISPLAY_COMMENT = 4

    STATUS_SHOW = 1
    STATUS_HIDE = 0
    STATUS_ITEMS = (
        (STATUS_SHOW, 'Show'),
        (STATUS_HIDE, 'Hide')
    )
    SIDE_TYPE = (
        (DISPLAY_HTML, 'HTML'),
        (DISPLAY_LATEST, '最新文章'),
        (DISPLAY_HOT, '最热文章'),
        (DISPLAY_COMMENT, '最近评论'),
    )
    title = models.CharField(max_length=50, verbose_name='Title')
    display_type = models.PositiveIntegerField(default=1, choices=SIDE_TYPE, verbose_name='Display Type')
    content = models.CharField(max_length=500, blank=True, verbose_name='Content', help_text='not html type, is blank')
    status = models.PositiveIntegerField(default=STATUS_SHOW, choices=STATUS_ITEMS, verbose_name='Status')
    owner = models.ForeignKey(User, verbose_name='Author', on_delete=models.DO_NOTHING)
    created_time = models.DateTimeField(auto_now_add=True, editable=False, verbose_name='Create Time')

    class Meta:
        verbose_name = verbose_name_plural = 'Side Bar'

    @classmethod
    def get_all(cls):
        return cls.objects.filter(status=cls.STATUS_SHOW)

    @property
    def content_html(self):
        from blog.models import Post
        from comment.models import Comment

        result = ''
        if self.display_type == self.DISPLAY_HTML:
            result = self.content
        elif self.display_type == self.DISPLAY_LATEST:
            context = {
                'posts': Post.latest_posts()
            }
            result = render_to_string('config/blocks/sidebar_posts.html', context)
        elif self.display_type == self.DISPLAY_COMMENT:
            context = {
                'comments': Comment.objects.filter(status=Comment.STATUS_NORMAL).order_by('-pk')[:5]
            }
            result = render_to_string('config/blocks/sidebar_comments.html', context)
        elif self.display_type == self.DISPLAY_HOT:
            context = {
                'posts': Post.hot_posts()[:10]
            }
            result = render_to_string('config/blocks/sidebar_hot_posts.html', context)

        return result
